package pkg16b_eca.SQL.Tables;

import java.util.ArrayList;
import java.util.HashMap;
import pkg16b_eca.SQL.Query;
import se.oru.informatik.InformatikException;

public class SQLAgent extends Query {
    public SQLAgent() throws InformatikException {}
    
    /** Inserting values into database. */
    public void insert(final String strNamn, final String strTelefon, final String strMail) {
        String query = "insert into agent (AID, namn, telefon, mail) values (" + 
                SQLGetAutoIncrement("agent", "AID") + ", '" + strNamn + "', '" + strTelefon + "', '" + strMail + "')";
        SQLInsert(query);
    }
    
    /** Inserting values into database. */
    public void update(final int intAID, final String strNamn, final String strTelefon, final String strMail) {
        String query = "update agent set namn='" + strNamn + "', telefon='" + strTelefon + "', mail='" + strMail + "' where AID=" + intAID;
        SQLUpdate(query);
    }
    
    /** Deleting a value from the database. */
    public void delete(final int intAID) {
        String query = "delete from agent where AID=" + intAID;
        SQLDelete(query);
    }
    
    /** Check if a value exists. */
    public boolean exists(final String AName) {
        String query = SQLSelect("select namn from agent where namn = '" + AName + "'");
        return !query.isEmpty();
    }
    
    /** Check if a value exists. */
    public boolean exists(final int AID) {
        String query = SQLSelect("select AID from agent where AID = '" + AID + "'");
        return !query.isEmpty();
    }
    
    /**Usage Example : Se vilka agenter det finns
     *Variables used: al = ArrayList, s = String
        ArrayList<String> al = SQL.getAgent().getAgenter();
        for (String s : al) {
            System.out.println(s);
        }
     */
    public ArrayList<String> getAgenter() {
        return SQLSelectList("select namn from agent");
    }
    
    /**Usage Example : Se vilka attribut en agent har
     *Variables used: hm = HashMap
        HashMap<String, String> hm = SQL.getAgent().getAttribut("Christoffer Johansson");
        System.out.println(hm.values());
     */
    public HashMap<String, String> getAttribut(String agentName) {
        return SQLSelectRow("select * from agent " + 
                "where agent.namn = '" + agentName + "'");
    }
    
    /**Usage Example : Se vilka skådespelare en agent representerar
     *Variables used: al = ArrayList
        ArrayList<String> al = SQL.getAgent().getSkadespelare("Toni Anderberg");
        for (String s : al) {
            System.out.println(s);
        }
     */
    public ArrayList<String> getSkadespelare(String agentName) {
        return SQLSelectList("select skadespelare.namn from skadespelare " + 
                "join agent on agent.aid = skadespelare.aid " + 
                "where agent.namn = '" + agentName + "'");
    }
}
